/*
 * @Author: byAcya xiaoxianbin@4399inc.com
 * @Date: 2025-02-10 09:54:43
 * @LastEditors: byAcya xiaoxianbin@4399inc.com
 * @LastEditTime: 2025-02-11 10:53:19
 * @FilePath: \CocosFramework\assets\script\core\utils\DateUtils.ts
 * @Description: 日期格式化  utils
 */
import Instance from './Instance';

export type DateFormatType = 'YYYY-MM-DD hh:mm:ss' | 'YYYY-MM-DD' | 'YY-MM' | 'MM-DD' | 'hh:mm:ss' | 'hh:mm' | 'mm:ss';

export interface IDateUtils {
    //秒
    format(t: number, type: DateFormatType): string; //格式化时间
    getDay(t: number): string; //获取当前为星期几
    getDifferenceDay(t1: number, t2: number): number; //获取两个时间的间隔天数
    isSameDay(t1: number, t2: number); //判断两个时间是否是同一天
}

export class DateUtils implements IDateUtils {
    public static getInstance(): DateUtils {
        return Instance.get<DateUtils>(DateUtils);
    }
    format(t: number, type: DateFormatType = 'hh:mm:ss'): string {
        switch (type) {
            case 'YYYY-MM-DD hh:mm:ss':
                return this.formatYYMMDDHHMMSS(t);
            case 'YYYY-MM-DD':
                return this.formatYYMMDD(t);
            case 'YY-MM':
                return this.formatYYMM(t);
            case 'MM-DD':
                return this.formatMMDD(t);
            case 'hh:mm:ss':
                return this.formatHHMMSS(t);
            case 'hh:mm':
                return this.formatHHMM(t);
            case 'mm:ss':
                return this.formatMMSS(t);
            default:
                return this.formatHHMMSS(t);
        }
    }

    private formatHHMMSS(t: number): string {
        let h = Math.floor(t / 3600);
        let m = Math.floor((t - h * 3600) / 60);
        let s = Math.floor(t - h * 3600 - m * 60);
        const hh = h == 0 ? '00' : h < 10 ? '0' + h : h.toString();
        const mm = m == 0 ? '00' : m < 10 ? '0' + m : m.toString();
        const ss = s == 0 ? '00' : s < 10 ? '0' + s : s.toString();
        return hh + ':' + mm + ':' + ss;
    }
    private formatHHMM(t: number): string {
        let h = Math.floor(t / 3600);
        let m = Math.floor((t - h * 3600) / 60);
        const hh = h == 0 ? '00' : h < 10 ? '0' + h : h.toString();
        const mm = m == 0 ? '00' : m < 10 ? '0' + m : m.toString();
        return hh + ':' + mm;
    }

    private formatMMSS(t: number): string {
        let m = Math.floor(t / 60);
        let s = Math.floor(t - m * 60);
        const mm = m == 0 ? '00' : m < 10 ? '0' + m : m.toString();
        const ss = s == 0 ? '00' : s < 10 ? '0' + s : s.toString();
        return mm + ':' + ss;
    }

    private formatYYMMDDHHMMSS(t: number): string {
        const date = new Date(t * 1000);
        const y = date.getFullYear();
        const m = date.getMonth() + 1; //月份  0-11
        const d = date.getDate();
        const _h = date.getHours();
        const _m = date.getMinutes();
        const _s = date.getSeconds();
        const _hh = _h == 0 ? '00' : _h < 10 ? '0' + _h : _h.toString();
        const _mm = _m == 0 ? '00' : _m < 10 ? '0' + _m : _m.toString();
        const _ss = _s == 0 ? '00' : _s < 10 ? '0' + _s : _s.toString();
        return y + '-' + m + '-' + d + ' ' + _hh + ':' + _mm + ':' + _ss;
    }

    private formatYYMMDD(t: number): string {
        const date = new Date(t);
        const y = date.getFullYear();
        const m = date.getMonth() + 1; //月份  0-11
        const d = date.getDate();
        return y + '-' + m + '-' + d;
    }

    private formatYYMM(t: number): string {
        const date = new Date(t);
        const y = date.getFullYear();
        const m = date.getMonth() + 1; //月份  0-11
        return y + '-' + m;
    }

    private formatMMDD(t: number): string {
        const date = new Date(t);
        const m = date.getMonth() + 1; //月份  0-11
        const d = date.getDate();
        return m + '-' + d;
    }

    /**
     * 获取星期几
     * @param t 时间戳
     * @returns 星期几  ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]
     */
    getDay(t: number): string {
        const date = new Date(t * 1000);
        const d = date.getDay();
        return ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'][d];
    }

    /**
     * 获取两个时间戳相差的天数
     * @param t1 时间戳1
     * @param t2 时间戳2
     * @returns 相差天数
     */
    getDifferenceDay(t1: number, t2: number): number {
        const v = Math.abs(t1 - t2);
        return Math.ceil(v / (24 * 60 * 60 * 1000));
    }

    /**
     * 判断两个时间戳是否为同一天
     * @param t1 时间戳1
     * @param t2 时间戳2
     * @returns 是否为同一天
     * */
    isSameDay(t1: number, t2: number) {
        const d1: Date = new Date(t1 * 1000);
        const d2: Date = new Date(t2 * 1000);
        return d1.getFullYear() == d2.getFullYear() && d1.getMonth() == d2.getMonth() && d1.getDate() == d2.getDate();
    }
}
